/**
 * 
 */
package com.webclient;

import java.awt.BorderLayout;
import java.awt.Cursor;
import java.awt.GridLayout;
import java.awt.Toolkit;
import java.awt.Window;
import java.awt.geom.RoundRectangle2D;
import java.lang.reflect.InvocationTargetException;
import java.text.ParseException;

import javax.swing.ImageIcon;
import javax.swing.JFrame;
import javax.swing.SwingUtilities;
import javax.swing.UIManager;
import javax.swing.UnsupportedLookAndFeelException;
import javax.swing.border.EmptyBorder;
import javax.swing.plaf.synth.SynthLookAndFeel;

import com.sun.awt.AWTUtilities;
import com.webclient.base.BaseContentPanel;
import com.webclient.components.MainFrameTitlelBar;
import com.webclient.swingext.CursorUIResource;

/**
 *  Web应用客户端程序
 * qq:297051898
 * @author luxf
 */
public class WebClent extends JFrame {
	
	private static final long serialVersionUID = 1L;
	protected Toolkit toolkit = Toolkit.getDefaultToolkit();
    /** 系统配置 */
	public static final SystemConfig config = SystemConfig.getConfig();
	
	public WebClent() {
		setSize(config.getDefaultWidth(), config.getDefaultHeight());
		setLocationRelativeTo(null);
		setContentPane(new ContentPanel());
		setUndecorated(true);
		setResizable(true);
	}
	
	public static void main(String[] args) throws ParseException, UnsupportedLookAndFeelException, InterruptedException, InvocationTargetException {
		// 初始化皮肤
		initialSkin();
		// 初始化应用组件
		initialComponents();
	}

	private static void initialComponents() throws InterruptedException,
			InvocationTargetException {
		final WebClent webClent = new WebClent();
		webClent.setIconImage(((ImageIcon)UIManager.get("System.logo")).getImage());
		webClent.setLayout(new BorderLayout());
		webClent.add(new MainFrameTitlelBar(webClent), BorderLayout.NORTH);
		
		GridLayout layout = new GridLayout(1, 1);
		layout.setHgap(10);
		layout.setVgap(10);
		SwingUtilities.invokeAndWait(new Runnable() {
			@Override
			public void run() {
				// 创建浏览器面板并添加到窗体当中
				WebPanel webPanel = new WebPanel();
				webClent.add(webPanel, BorderLayout.CENTER);
				webPanel.setBorder(new EmptyBorder(0, 10, 20, 10)); 
				webPanel.load(config.getDefaultUrl());
			}
		});
		
		webClent.setVisible(true);
		// 设置窗体是否不透明
		AWTUtilities.setWindowOpaque(webClent, true);
		// 设置窗体圆角 20个像索的圆角
		AWTUtilities.setWindowShape(webClent, new RoundRectangle2D.Double(
				0, 0, webClent.getWidth(), webClent.getHeight(), config.getArcX(), config.getArcY()));
	}

	private static void initialSkin() throws ParseException,
			UnsupportedLookAndFeelException {
		SynthLookAndFeel synthLookAndFeel = new SynthLookAndFeel();
		synthLookAndFeel.load(WebClent.class.getResourceAsStream(
				"/skin/" + config.getDefaultSkin() + "/" +  config.getDefaultSkin() + ".xml"), WebClent.class);
		UIManager.setLookAndFeel(synthLookAndFeel);
		UIManager.put("Button.font", new CursorUIResource(Cursor.HAND_CURSOR));
	}
	
	public class ContentPanel extends BaseContentPanel {
		private static final long serialVersionUID = 1L;
	}
	
	/**
	 * 设置窗体圆角
	 * @param window
	 */
	public static void setWindowShape(Window window) {
		AWTUtilities.setWindowShape(window, new RoundRectangle2D.Double(
				0, 0, window.getWidth(), window.getHeight(), config.getArcX(), config.getArcY()));
	}
}
